package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Course;
import com.jyy.prefabricated.entity.CoursePackage;
import com.jyy.prefabricated.entity.ProjectInfo;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.enums.UserTypeEnum;
import com.jyy.prefabricated.service.MyResourceService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.StudentExamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的资源
 *
 * @version 1.0.0
 * @author: zjj
 */
@RequestMapping("/api")
@RestController
public class MyResourceController {

    @Autowired
    private MyResourceService myResourceService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 查询我的项目
     *
     * @param name     名称
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/my-project")
    public AjaxResult findMyProject(@RequestParam(required = false, defaultValue = "") String name, Pageable pageable) {
        SysUser user = userUtils.getCurrentUser();
        List<ProjectInfo> list = new ArrayList<>();
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        if (UserTypeEnum.TRAININGORGANIZATION.getValue().equals(user.getUserType())) {
            list = myResourceService.selectByProjectNameAndTrainOrganizationUserId(name, user.getId());
        } else if (UserTypeEnum.STUDENT.getValue().equals(user.getUserType())) {
            list = myResourceService.selectByProjectNameAndStudentId(name, user.getStudent().getId());
        }
        PageInfo<ProjectInfo> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 查询我的课程
     *
     * @param name     名称
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/my-course")
    public AjaxResult findMyCourse(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer type, Pageable pageable) {
        SysUser user = userUtils.getCurrentUser();
        List<Integer> courseIds = new ArrayList<>();
        if (UserTypeEnum.TRAININGORGANIZATION.getValue().equals(user.getUserType())) {
            courseIds = myResourceService.selectCourseIdsByTrainOrganizationUserId(user.getId());
        } else if (UserTypeEnum.STUDENT.getValue().equals(user.getUserType())) {
            courseIds = myResourceService.selectCourseIdsByStudentId(user.getStudent().getId());
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Course> list = myResourceService.selectByCourseNameAndCourseIds(name, type, courseIds);
        PageInfo<Course> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 查询我的课程包
     *
     * @param name     名称
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/my-coursePackage")
    public AjaxResult findMyCoursePackage(@RequestParam(required = false, defaultValue = "") String name, Pageable pageable) {
        SysUser user = userUtils.getCurrentUser();
        List<Integer> coursePackageIds = new ArrayList<>();

        if (UserTypeEnum.TRAININGORGANIZATION.getValue().equals(user.getUserType())) {
//            coursePackageIds = myResourceService.selectCoursePackageIdsByStudentId(user.getId());
        } else if (UserTypeEnum.STUDENT.getValue().equals(user.getUserType())) {
            coursePackageIds = myResourceService.selectCoursePackageIdsByStudentId(user.getStudent().getId());
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CoursePackage> list = myResourceService.selectByCoursePackageNameAndCourseIds(name, coursePackageIds);
        PageInfo<CoursePackage> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 查询我的考试
     *
     * @param name     名称
     * @param type     type=2查询企业自己组卷的考试
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/my-Exam")
    public AjaxResult findMyExam(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer type, Pageable pageable) {
        SysUser user = userUtils.getCurrentUser();
        PageInfo<StudentExamVo> list = new PageInfo<>();
        if (UserTypeEnum.STUDENT.getValue().equals(user.getUserType())) {
            PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
            list = myResourceService.selectExamByStudentId(name, user.getStudent().getId(), type);
        }
//        PageInfo<StudentExamVo> result = new PageInfo<>(list);

        return AjaxResult.success(list);
    }


}
